package com.tanhua.server.controller;

import com.tanhua.model.mongo.Answers;
import com.tanhua.model.mongo.TestSoulVo;
import com.tanhua.model.vo.ReportVo;
import com.tanhua.server.service.TestSoulService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/testSoul")
@RestController
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /*
     *  测灵魂-问卷列表（学生实战）
     *    请求方式和请求链接 : GET
     *    请求参数:
     *    返回值: List<TestSoulVo> list;
     * */

    @GetMapping
    public ResponseEntity testSoul() {

        List<TestSoulVo> list = testSoulService.findAll();

        return ResponseEntity.ok(list);
    }


    /*
     *  测灵魂-提交问卷（学生实战）
     *    请求方式和请求链接 : POST
     *    请求参数:  answers
     *    返回值: 	string  报告id
     * answers
     * */
    @PostMapping
    public ResponseEntity testSoul(@RequestBody Map map) {

        //先拿list接受
        List<Map> answers = (ArrayList) map.get("answers");

        List<Answers> list = new ArrayList<>();
        for (Map answer : answers) {
            String questionId = (String) answer.get("questionId");
            String optionId = (String) answer.get("optionId");
            Answers ans = new Answers();
            ans.setQuestionId(questionId);
            ans.setOptionId(optionId);
            list.add(ans);
        }

        String resultId = testSoulService.submitQuestion(list);

        return ResponseEntity.ok(resultId);

    }


    /*
     *   测灵魂-查看结果（学生实战）
     *    请求方式和请求链接 :GET   /report/:id
     *    请求参数:  id  报告id
     *    返回值:  ReportVo对象
     * */
    @GetMapping("/report/{id}")
    public ResponseEntity report(@PathVariable("id") String id){

        ReportVo vo  =testSoulService.report(id);

        return ResponseEntity.ok(vo);

    }


}
